package com.ems.service.impl.Invigilate.room;

import com.ems.dao.Invigilate.room.TestScoreResultDao;
import com.ems.pojo.Invigilate.room.TestResult;
import com.ems.pojo.Invigilate.room.TestScoreResult;
import com.ems.pojo.data.ResultData;
import com.ems.pojo.statistics.Referer;
import com.ems.pojo.statistics.ScoreStatistics;
import com.ems.pojo.user.User;
import com.ems.service.Invigilate.room.TestResultService;
import com.ems.service.Invigilate.room.TestScoreResultService;
import com.ems.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 伟伟
 * @Date: 2020/3/12
 * @Description: 描述
 * @Version: 1.0
 */
@Service("testScoreResultService")
public class TestScoreResultServiceImpl implements TestScoreResultService {
    @Autowired
    private TestScoreResultDao testScoreResultDao;

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Autowired
    @Qualifier("testResultService")
    private TestResultService testResultService;


    @Override
    public void addTestScoreResult(TestScoreResult testScoreResult) {
        testScoreResultDao.addTestScoreResult(testScoreResult);
    }

    //成绩查询
    @Override
    public List<TestScoreResult> scoreStatistics(Map<String, Object> map) {
        List<TestScoreResult> list = testScoreResultDao.scoreStatistics(map);
        TestScoreResult testScoreResult = null;
        for(int i = 0;i < list.size();i ++){
             testScoreResult = list.get(i);
             testScoreResult.setTestResultList(null);
             testScoreResult.setUser(userService.getUserById(testScoreResult.getId()));
        }
        return list;
    }

    //分数统计
    @Override
    public List<Referer> getRefererInfo(TestScoreResult testScoreResult) {
        List<Referer> refererList = new ArrayList<>();

        //查询0到59分以下的人数
        refererList.add(new Referer("0到59分人数",testScoreResultDao.getRefererInfoCount(0,59,testScoreResult.getRp_id())));

        //查询60分到69分的人数
        refererList.add(new Referer("60到69分人数",testScoreResultDao.getRefererInfoCount(60,69,testScoreResult.getRp_id())));

        //查询70到79的人数
        refererList.add(new Referer("70到79分人数",testScoreResultDao.getRefererInfoCount(70,79,testScoreResult.getRp_id())));
        //查询80到89的人数
        refererList.add(new Referer("80到89分人数",testScoreResultDao.getRefererInfoCount(80,89,testScoreResult.getRp_id())));
        //查询90分以上的人数
        refererList.add(new Referer("90分及以上人数",testScoreResultDao.getRefererInfoCount2(90,testScoreResult.getRp_id())));
        return refererList;
    }

    //删除学生考试信息
    @Override
    public void deleteTestScoreResultByRpId(int rp_id) {
        //查询具体的testScoreResult信息
        List<TestScoreResult> list = testScoreResultDao.getTestScoreResultByRpId(rp_id);

        //删除testResult信息
        for(int i = 0;i < list.size();i ++){
            testResultService.deleteTestResult(list.get(i).getTsr_id());
        }

        //删除testScoreResult
        for(int i = 0;i < list.size();i ++){
            testScoreResultDao.deleteTestScoreResultByRpId(list.get(i).getRp_id());
        }
    }


    @Override
    public ScoreStatistics getScoreStatistics(TestScoreResult testScoreResult) {
        List<TestScoreResult> testScoreResultList = testScoreResultDao.getTestScoreResultByRpId(testScoreResult.getRp_id());
        ScoreStatistics scoreStatistics = new ScoreStatistics();
        float count = 0;
        if(testScoreResultList.size() > 0){
            for(int i = 0;i < testScoreResultList.size();i ++){
                List<TestResult> testResultList = testResultService.getTestReusltByTsrId(testScoreResultList.get(i).getTsr_id());
                for(int j = 0;j < testResultList.size();j ++){
                    count += testResultList.get(j).getTr_score();
                    if(testResultList.get(j).getTr_type() == 1){
                        scoreStatistics.setChoose(scoreStatistics.getChoose() + testResultList.get(j).getTr_score());
                    }else if(testResultList.get(j).getTr_type() == 2){
                        scoreStatistics.setChooses(scoreStatistics.getChooses() + testResultList.get(j).getTr_score());
                    }else if(testResultList.get(j).getTr_type() == 3){
                        scoreStatistics.setJudgment(scoreStatistics.getJudgment() + testResultList.get(j).getTr_score());
                    }else if(testResultList.get(j).getTr_type() == 4){
                        scoreStatistics.setFillBanks(scoreStatistics.getFillBanks() + testResultList.get(j).getTr_score());
                    }
                }
            }
            scoreStatistics.setPeopleNum(testScoreResultList.size());
            scoreStatistics.setChoose(scoreStatistics.getChoose() / testScoreResultList.size());
            scoreStatistics.setChooses(scoreStatistics.getChooses() / testScoreResultList.size());
            scoreStatistics.setJudgment(scoreStatistics.getJudgment() / testScoreResultList.size());
            scoreStatistics.setFillBanks(scoreStatistics.getFillBanks() / testScoreResultList.size());
        }

        if(count > 0){
            scoreStatistics.setScore(count / scoreStatistics.getPeopleNum());
        }
        return scoreStatistics;
    }

    @Override
    public TestScoreResult getScoreStatisticsByRpIdAndId(TestScoreResult testScoreResult) {
        TestScoreResult testScoreResult1 = testScoreResultDao.getTestScoreResultByRpIdAndId(testScoreResult.getRp_id(),testScoreResult.getId());
        User user = userService.getUserById(testScoreResult.getId());
        if(testScoreResult1 != null){
            testScoreResult1.setUser(user);
        }else{
            testScoreResult1 = new TestScoreResult();
            testScoreResult1.setUser(user);
        }

        return testScoreResult1;
    }

    @Override
    public void deleteTestScoreResultById(List<User> list) {
        if(list != null && list.size() > 0){
            User user = null;
            for(int i = 0;i < list.size();i ++){
                user = list.get(i);
                List<TestScoreResult> testScoreResultList = testScoreResultDao.queryTsrById(user.getId());
                for(int j = 0;j < testScoreResultList.size();j ++){
                    deleteTestScoreResultByRpId(testScoreResultList.get(j).getRp_id());
                }
            }
        }
    }

    //根据用户id和考卷的rp_id查询
    @Override
    public TestScoreResult examinationStatusQuery(int id, int rp_id) {
        return testScoreResultDao.examinationStatusQuery(id,rp_id);
    }
}
